<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据库管理</title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<script src="js/page.js"></script>
<script type="text/javascript">
<!--
function submitHandler()
{
	if (document.getElementById("host_address").value.replace(/(^\s*)|(\s*$)/g, "") == "")
	{
		window.alert("请输入服务器地址！");
		document.getElementById("host_address").focus();
	}
	else if (document.getElementById("port_custom").checked == true && document.getElementById("host_port").value.replace(/(^\s*)|(\s*$)/g, "") == "")
	{
		window.alert("请输入自定义端口号！");
		document.getElementById("host_port").focus();
	}
	else if (document.getElementById("host_username").value.replace(/(^\s*)|(\s*$)/g, "") == "")
	{
		window.alert("请输入用户名！");
		document.getElementById("host_username").focus();
	}
	else if (document.getElementById("host_password").value.replace(/(^\s*)|(\s*$)/g, "") == "")
	{
		window.alert("请输入密码！");
		document.getElementById("host_password").focus();
	}
	else
		document.getElementById("form_server").submit();
	return false;
}
//-->
</script>
</head>

<body onload="initialize();document.getElementById('host_address').focus();">
<jsp:useBean id="database" class="DatabaseConnection.DatabaseConnection" />
<%
String setup_type = request.getParameter("type");
if (setup_type == null || !setup_type.equals("new"))
{
	Cookie cookies[] = request.getCookies();
	for (int i = 0; cookies != null && i < cookies.length; i++)
	{
		Cookie current_cookie = cookies[i];
		String remember_setup_string = "";
		if (current_cookie.getName().equals("mysql_remember_setup"))
		{
			remember_setup_string = current_cookie.getValue();
			String [] remember_setups = remember_setup_string.split(",");
			String host_address = remember_setups[0];
			String host_port = remember_setups[1];
			String host_username = remember_setups[2];
			String host_password = remember_setups[3];
			database.initDatabase(host_address, host_port, host_username, host_password);
			String sql_databases = "show databases;";
			ResultSet resultset_databases = database.executeQuery(sql_databases);
			if (resultset_databases != null)
			{
				resultset_databases.close();
				database.closeConnection();
				session.putValue("mysql_host_address", host_address);
				session.putValue("mysql_host_port", host_port);
				session.putValue("mysql_host_username", host_username);
				session.putValue("mysql_host_password", host_password);
				response.sendRedirect("databases.jsp");
				break;
			}
			else
				database.closeConnection();
		}
	}
}
else
{
	session.invalidate();
	Cookie delete_cookie = new Cookie("mysql_remember_setup", null);
	delete_cookie.setMaxAge(0);
	response.addCookie(delete_cookie);
}
%>
<div style="padding-top:60px" />
<form id="form_server" name="form_server" method="post" action="connection_do.jsp" onsubmit="return submitHandler();">
  <table width="80%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#B1C8D7">
    <tr align="center" bgcolor="#F4F4FF">
      <td height="30" colspan="2" style="line-height:30px;"><font size="+1"><strong>数据库服务器设置</strong></font></td>
    </tr>
    <tr align="center" bgcolor="#FFFFFF">
      <td width="30%" height="24">服务器地址</td>
      <td height="24"><input name="host_address" type="text" id="host_address" style="width:300px;" /></td>
    </tr>
    <tr align="center" bgcolor="#FFFFFF">
      <td height="24">端口</td>
      <td height="24">
        <input name="port_type" type="radio" id="port_default" value="default" checked="checked" onclick="document.getElementById('host_port').disabled=true;" /><label for="port_default">默认端口</label>
        <input name="port_type" type="radio" id="port_custom" value="custom" onclick="document.getElementById('host_port').disabled=false; document.getElementById('host_port').focus();" /><label for="port_custom">自定义</label>
        <input name="host_port" type="text" id="host_port" style="width:163px;" disabled="disabled" />
      </td>
    </tr>
    <tr align="center" bgcolor="#FFFFFF">
      <td height="24">用户名</td>
      <td height="24"><input name="host_username" type="text" id="host_username" style="width:300px;" /></td>
    </tr>
    <tr align="center" bgcolor="#FFFFFF">
      <td height="24">密码</td>
      <td height="24"><input name="host_password" type="password" id="host_password" style="width:300px;" /></td>
    </tr>
    <tr align="center" bgcolor="#F4F4FF">
      <td height="24" colspan="2">
        <input name="remember_setup" type="checkbox" id="remember_setup" value="remember" /><label for="remember_setup">记住此设置</label>
        　　
        <input name="ok" type="submit" class="input_button" id="ok" value="确定" />
        　
        <input name="reset" type="reset" class="input_button" id="reset" onclick="document.getElementById('host_address').focus();" value="重置" />
      </td>
    </tr>
  </table>
</form>
</body>
</html>